package top.yxqz.spring.ts.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import top.yxqz.spring.ts.dao.BookDao;

/**
 * @author: 余小小
 * @date: 2023/4/18
 * @description：
 */
@Repository
public class BookDaoImpl implements BookDao {

    //    装配JDBCTemplate对象
    //    @Qualifier("jdbcTemplate")
    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 查询图书价格
     *
     * @param bookId
     * @return
     */
    public Integer getPriceByBook(Integer bookId) {
        return jdbcTemplate.queryForObject("select price from t_book where book_id =?", Integer.class,bookId);
    }

    /**
     * 更新库存
     *
     * @param bookId
     */
    @Override
    public void updateStock(Integer bookId) {
        jdbcTemplate.update("update t_book set stock = stock - 1 where book_id =?", bookId);
    }

    /**
     * 更新用户余额
     *
     * @param userId
     * @param price
     */
    @Override
    public void updateBalance(Integer userId, Integer price) {
        jdbcTemplate.update("update t_book_user set  balance= balance - ? where user_id =?", price, userId);
    }
}
